define(function () {
    'use strict';

    function ctrl($scope, $filter, $stateParams, InfoService,$ionicScrollDelegate,CommonService, ConcernService) {

        $scope.channel = {};
        $scope.vm = {};
        $scope.channel.channelId = $stateParams['id'];
        $scope.index = $stateParams['index'];
        $scope.title = $scope.index ? "银行理财" : "银行宝";
        init();

        $scope.doRefresh = function() {
            init(true);
            $scope.$broadcast('scroll.refreshComplete');

        };

        $scope.loadMore = function(refresh) {
            if(refresh) {
                $scope.page_no = 1;
                $scope.items = new Array();
            }
            return queryContentBriefs();
        };

        $scope.goTop = function(){
            $ionicScrollDelegate.scrollTop(true);
        }

        $scope.operUserCare = function(prod_code){
            ConcernService.concern(prod_code);
        }

        function init(refresh){
            if(refresh) {
                $scope.vm.loadMore = true;
                $scope.loadMore(refresh);
            } else {
                $scope.items = new Array();
                $scope.page_no = 1;
                $scope.vm.loadMore = true;
            }

        }

        function queryContentBriefs(){
            return InfoService.getProductsByParent( $scope.channel.channelId, $scope.page_no).then(function(data){
                if(data){
                    if(data && data.length > 0){
                        for(var i=0;i<data.length;i++){
                            var fund = data[i];
                            fund.prod_min_subscribe = $filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                            if($scope.index) {
                                fund.concern =  ConcernService.isConcern(fund.prod_code);
                            }
                        }
                        $scope.items = $scope.items.concat(data);
                        $scope.page_no++;
                    }else {
                        if($scope.page_no == 1) {
                            $scope.showNo = true;
                        }
                        $scope.vm.loadMore = false;
                    }
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                }
            });
        }
    }

    ctrl.$inject = ['$scope','$filter','$stateParams','InfoService','$ionicScrollDelegate','CommonService','ConcernService'];
    return ctrl;

});